class <%= identity_class_name %> < ActiveRecord::Base
  include Socialite::Models::IdentityConcern

  belongs_to :user,
    :class_name => Socialite.user_class_name,
    :foreign_key => :<%= singular_user_name %>_id

  serialize :auth_hash

  # Ensure that before validation happens that the provider
  # database column matches what is inside of the auth_hash
  # dataset.
  before_validation do |identity|
    if identity.auth_hash.present?
      identity.provider = identity.auth_hash.delete('provider') if identity.provider.blank?
      identity.uid = identity.auth_hash.delete('uid') if identity.uid.blank?
    end
  end

  # Ensure each user has only a single identity per provider type
  validates :provider,
    :uniqueness => {
    :scope => :<%= singular_user_name %>_id,
      :case_sensitive => false
    },
    :presence => true

  # Ensure an identity is never reused by another account
  validates :uid,
    :uniqueness => {:scope => :provider},
    :presence => true

  # Ensure an associated user exists before creating the identity
  # validates_associated :user
  
  # Finder method that finds the matching Provider and Unique ID or
  # initializes a new, unsaved, object.
  #
  # @params [Hash] the OAuth authentication hash
  # @returns [Identity]
  def self.create_from_omniauth(auth={})
    super
  end

  def self.find_or_create_from_omniauth(auth)
    super
  end
  
  # Finder method that finds the matching Provider and UID.
  #
  # @params [Hash] the OmniAuth authentication hash
  def self.find_from_omniauth(auth={})
    super
  end

  # Convenience method for accessing the OAuth access token
  #
  # @returns [String] OAuth access token
  # (see #credentials)
  def access_token
    super
  end

  # Convenience method for accessing the OAuth access token secret
  #
  # @returns [String] OAuth access token secret
  # (see #credentials)
  def access_token_secret
    super
  end

  # Convenience method for accessing the OAuth credentials sub-hash
  #
  # @returns [Hash] OAuth credentials sub-hash
  # (see #access_token)
  # (see #access_token_secret)
  def credentials
    super
  end

  # Convenience method for accessing the nickname, which is typically
  # set to the login name used for that provider.
  #
  # @returns [String] user nickname for the provider identity
  def nickname
    super
  end

  # Convenience method for accessing the user information from the
  # OAuth provider.
  #
  # @returns [Hash] the user information sub-hash
  def info
    super
  end
end
